<?php

include_once('php/register.php');

include_once('php/generic_include.php');

function log_in() { // if return 0, user is logged.
  if (isset($_POST['login']) AND isset($_POST['pwd'])) {
    $login = clear($_POST['login']);
    $pwd = clear($_POST['pwd']);
    $hash = md5($pwd);
    $size = strlen($login);
    $member = mysql_query("SELECT * FROM members WHERE login='$login' AND pwd='$hash'");
    while ($row = mysql_fetch_assoc($member)) {
      if ($row['login'] == $login AND $row['pwd'] == $hash) {
	$_SESSION['logged'] = md5($size . $login . $hash);
	if (!is_active($login)) {
	  active($login);
	}
	return 0;
      }
    return 1;
    }
  }
  else {
    return 1;
  }
}

function active($login) {
  mysql_query('UPDATE members SET active=TRUE WHERE login=' . $login);
}

function is_active($login) {
  $active = mysql_query("SELECT active FROM members WHERE login='$login'");
  while ($row = mysql_fetch_assoc($active)) {
    if ($row['active']) {
      return TRUE;
    }
    else {
      return FALSE;
    }
  }
}

function is_logged() {
  if (isset($_COOKIE['connected'])
      OR isset($_SESSION['logged'])) {
    $members = mysql_query('SELECT * FROM members');
    while ($row = mysql_fetch_assoc($members)) {
      $login = $row['login'];
      $pwd = $row['pwd'];
      $size = strlen($login);
      if ($_COOKIE['connected'] == md5($size . $login . $pwd)
	  OR $_SESSION['logged'] == md5($size . $login . $pwd)) {
	return TRUE;
      }
    }
    return FALSE;
  }
  else {
    return FALSE;
  }
}

?>